#include "page1_video.h"
#include "ui_page1_video.h"

page1::page1(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::page1)
{
    ui->setupUi(this);
    this->setFixedSize(1280,720);

//################################################
//#                                              #
//#           设置界面1背景                        #
//#           最后一次修改4.9，11：16               #
//#                                              #
//################################################
    QPixmap widget_background = QPixmap(":/ocean.jpg");
    widget_background = widget_background.scaled(1280,720,Qt::KeepAspectRatio);
    QPixmap temp_background(widget_background.size());
    temp_background.fill(Qt::transparent);
    //白色透明度
    //    QPainter picture_background(&temp_background);
    //    picture_background.setCompositionMode(QPainter::CompositionMode_Source);
    //    picture_background.drawPixmap(0,0,widget_background);
    //    picture_background.setCompositionMode(QPainter::CompositionMode_DestinationIn);

    //    picture_background.fillRect(widget_background.rect(),QColor(0,0,0,150));
    //    picture_background.end();

    //    widget_background = temp_background;
    //黑色透明度
    QPainter picture_background(&widget_background);
    picture_background.setCompositionMode(QPainter::CompositionMode_Source);
    picture_background.drawPixmap(0,0,widget_background);
    picture_background.setCompositionMode(QPainter::CompositionMode_DestinationIn);

    picture_background.fillRect(widget_background.rect(),QColor(0,0,0,180));
    picture_background.end();

    ui->label_background->setPixmap(widget_background);

//################################################
//#                                              #
//#           视频显示相关函数                      #
//#           最后一次修改4.9，8：17                #
//#                                              #
//################################################
    this->timer_for_video = new QTimer(this);
    QObject::connect(timer_for_video,&QTimer::timeout,this,&page1::show_video);
//set label style
    ui->label_camera->setStyleSheet("color:rgb(255,255,255);");


//################################################
//#                                              #
//#           命令下发相关初始化                    #
//#           最后一次修改4.25，22：23              #
//#                                              #
//################################################
    this->timer_for_move = new QTimer(this);
    QObject::connect(timer_for_move,&QTimer::timeout,this,&page1::print_move_data);
}

page1::~page1()
{
    delete ui;
}

//################################################
//#                                              #
//#           窗口切换相关函数                      #
//#           最后一次修改4.9，7：51                名#
//#                                              #
//################################################
void page1::on_pushButton_page1_clicked()
{
    emit pushbutton_page1_clicked();
}


void page1::on_pushButton_page2_clicked()
{
    emit pushbutton_page2_clicked();
}


void page1::on_pushButton_page3_clicked()
{
    emit pushbutton_page3_clicked();
}


void page1::on_pushButton_page4_clicked()
{
    emit pushbutton_page4_clicked();
}


//################################################
//#                                              #
//#           视频显示相关函数                      #
//#           最后一次修改4.9，8：17                #
//#                                              #
//################################################
void page1::on_pushButton_open_clicked()
{
    if(ui->pushButton_open->text() == "打开摄像头"){
        ui->pushButton_open->setText("关闭摄像头");

//##########################################################something wrong
//        capture.open(2);
        capture.open(0);
        if(!capture.isOpened()){
            qDebug()<<"摄像头未打开\n";
            return;
        }
        timer_for_video->start(30);
    }else if(ui->pushButton_open->text() == "关闭摄像头"){
        ui->pushButton_open->setText("打开摄像头");
        ui->label_camera->setText("摄像头未打开");
        capture.release();
        timer_for_video->stop();
    }
}

void page1::show_video()
{
    capture>>frame;
    //cv::Mat frame_temp = frame;           这样是浅拷贝，不是复制数据，是共享数据，改变frame_temp的值会同时改变frame的值。
    cv::Mat frame_temp;
    frame.copyTo(frame_temp);               //copy_to和clone是深拷贝，也就是我们所认为的复制
    cv::cvtColor(frame_temp,frame_temp,cv::COLOR_BGR2RGB);
    QImage image((unsigned char *)(frame_temp.data),frame_temp.cols,frame_temp.rows,QImage::Format_RGB888);
    ui->label_camera->setPixmap(QPixmap::fromImage(image));
    emit reload_frame();
//    ui->label_camera->resize(image.width(),image.height());
}

cv::Mat page1::read_now_frame(){
    return frame;
}


//################################################
//#                                              #
//#           控制命令相关函数                      #
//#           最后一次修改4.15，16：17              #
//#                                              #
//################################################

void page1::on_pushButton_shoubing_clicked()
{
    if(ui->pushButton_shoubing->text() == "打开自动发送运动数据"){
        ui->pushButton_shoubing->setText("关闭自动发送运动数据");
        timer_for_move->start(10);
    }else if(ui->pushButton_shoubing->text() == "关闭自动发送运动数据"){
        ui->pushButton_shoubing->setText("打开自动发送运动数据");
        timer_for_move->stop();
    }
}

void page1::print_move_data()
{
    QByteArray data_send;
    data_send.resize(17);
    data_send[0] = 0xFE;  //数据头
    data_send[1] = 0x23;  //消息ID
    data_send[2] = 12;  //数据长度：12
    //--------------平动相关-------------
        qint16 pingdong_x = (qint16)(ui->lineEdit_pingdong_x->text().toDouble()*10000);
        qint16 pingdong_y = (qint16)(ui->lineEdit_pingdong_y->text().toDouble()*10000);
        qint16 pingdong_z = (qint16)(ui->lineEdit_pingdong_z->text().toDouble()*10000);
        //传入x轴平动速度
        data_send[3] = pingdong_x;
        data_send[4] = pingdong_x>>8;
        //传入y轴平动速度
        data_send[5] = pingdong_y;
        data_send[6] = pingdong_y>>8;
        //传入z轴平动速度
        data_send[7] = pingdong_z;
        data_send[8] = pingdong_z>>8;
    //--------------转动相关-------------
        qint16 zhuandong_roll = (qint16)(ui->lineEdit_zhuandong_roll->text().toDouble()*10000);
        qint16 zhuandong_pitch = (qint16)(ui->lineEdit_zhuandong_pitch->text().toDouble()*10000);
        qint16 zhuandong_yaw = (qint16)(ui->lineEdit_zhuandong_yaw->text().toDouble()*10000);
        //传入roll转动速度
        data_send[9] = zhuandong_roll;
        data_send[10] = zhuandong_roll>>8;
        //传入pitch转动速度
        data_send[11] = zhuandong_pitch;
        data_send[12] = zhuandong_pitch>>8;
        //传入yaw转动速度
        data_send[13] = zhuandong_yaw;
        data_send[14] = zhuandong_yaw>>8;

    //异或校验
    data_send[15] = data_send[1];
    for(int i = 2;i <= 14;i++){
        data_send[15] = data_send[15] ^ data_send[i];
    }
    //帧尾
    data_send[16] = 0xFF;
    emit data_upup->signal_send_YunTai(data_send);
}

void page1::on_pushButton_deep_clicked()
{
    qint16 deep = (qint16)(ui->lineEdit_deep->text().toDouble()*10000);
    QByteArray data_send;
    data_send.resize(8);
    data_send[0] = 0xFE;  //数据头
    data_send[1] = 0x13;  //消息ID
    data_send[2] = 0x03;  //数据长度：3
    //模式
    data_send[3] = 0x10;
    //期望深度
    data_send[4] = deep;
    data_send[5] = deep>>8;
    //校验位
    data_send[6] = data_send[1] ^ data_send[2] ^ data_send[3] ^ data_send[4] ^ data_send[5];

    data_send[7] = 0xFF; //数据尾

    emit data_upup->signal_send_Sport(data_send);
}


void page1::on_pushButton_pingdong_clicked()
{
    qint16 pingdong_x = (qint16)(ui->lineEdit_pingdong_x->text().toDouble()*10000);
    qint16 pingdong_y = (qint16)(ui->lineEdit_pingdong_y->text().toDouble()*10000);
    qint16 pingdong_z = (qint16)(ui->lineEdit_pingdong_z->text().toDouble()*10000);
    QByteArray data_send;
    data_send.resize(12);
    data_send[0] = 0xFE;  //数据头
    data_send[1] = 0x13;  //消息ID
    data_send[2] = 0x07;  //数据长度：7
    //模式
    data_send[3] = 0x11;
    //传入x轴平动速度
    data_send[4] = pingdong_x;
    data_send[5] = pingdong_x>>8;
    //传入y轴平动速度
    data_send[6] = pingdong_y;
    data_send[7] = pingdong_y>>8;
    //传入z轴平动速度
    data_send[8] = pingdong_z;
    data_send[9] = pingdong_z>>8;
    //校验位
    data_send[10] = data_send[1];
    for(int i = 2;i <= 9;i++){
        data_send[10] = data_send[10] ^ data_send[i];
    }
    data_send[11] = 0xFF;
    emit data_upup->signal_send_Sport(data_send);
}


void page1::on_pushButton_zhuandong_clicked()
{
    qint16 zhuandong_roll = (qint16)(ui->lineEdit_zhuandong_roll->text().toDouble()*10000);
    qint16 zhuandong_pitch = (qint16)(ui->lineEdit_zhuandong_pitch->text().toDouble()*10000);
    qint16 zhuandong_yaw = (qint16)(ui->lineEdit_zhuandong_yaw->text().toDouble()*10000);
    QByteArray data_send;
    data_send.resize(12);
    data_send[0] = 0xFE;  //数据头
    data_send[1] = 0x13;  //消息ID
    data_send[2] = 0x07;  //数据长度：7
    //模式
    data_send[3] = 0x12;
    //传入roll转动速度
    data_send[4] = zhuandong_roll;
    data_send[5] = zhuandong_roll>>8;
    //传入pitch转动速度
    data_send[6] = zhuandong_pitch;
    data_send[7] = zhuandong_pitch>>8;
    //传入yaw转动速度
    data_send[8] = zhuandong_yaw;
    data_send[9] = zhuandong_yaw>>8;
    //校验位
    data_send[10] = data_send[1];
    for(int i = 2;i <= 9;i++){
        data_send[10] = data_send[10] ^ data_send[i];
    }
    data_send[11] = 0xFF;
    emit data_upup->signal_send_Sport(data_send);
}


void page1::on_pushButton_arm_shen_clicked()
{
    QByteArray data_send;
    data_send.resize(6);
    data_send[0] = 0xFE;  //数据头
    data_send[1] = 0x11;  //消息ID
    data_send[2] = 0x01;  //数据长度：1
    data_send[3] = 0x00;  //机械臂前伸
    data_send[4] = data_send[1] ^ data_send[2] ^ data_send[3];
    data_send[5] = 0xFF;
    emit data_upup->signal_send_JiXieBi_arm(data_send);
}


void page1::on_pushButton_arm_stop_clicked()
{
    QByteArray data_send;
    data_send.resize(6);
    data_send[0] = 0xFE;  //数据头
    data_send[1] = 0x11;  //消息ID
    data_send[2] = 0x01;  //数据长度：1
    data_send[3] = 0x01;  //机械臂停止
    data_send[4] = data_send[1] ^ data_send[2] ^ data_send[3];
    data_send[5] = 0xFF;
    emit data_upup->signal_send_JiXieBi_arm(data_send);
}


void page1::on_pushButton_arm_shou_clicked()
{
    QByteArray data_send;
    data_send.resize(6);
    data_send[0] = 0xFE;  //数据头
    data_send[1] = 0x11;  //消息ID
    data_send[2] = 0x01;  //数据长度：1
    data_send[3] = 0x02;  //机械臂回收
    data_send[4] = data_send[1] ^ data_send[2] ^ data_send[3];
    data_send[5] = 0xFF;
    emit data_upup->signal_send_JiXieBi_arm(data_send);
}


void page1::on_pushButton_hand_zhua_clicked()
{
    QByteArray data_send;
    data_send.resize(6);
    data_send[0] = 0xFE;  //数据头
    data_send[1] = 0x12;  //消息ID
    data_send[2] = 0x01;  //数据长度：1
    data_send[3] = 0x00;  //机械爪抓取
    data_send[4] = data_send[1] ^ data_send[2] ^ data_send[3];
    data_send[5] = 0xFF;
    emit data_upup->signal_send_JiXieBi_hand(data_send);
}


void page1::on_pushButton_hand_stop_clicked()
{
    QByteArray data_send;
    data_send.resize(6);
    data_send[0] = 0xFE;  //数据头
    data_send[1] = 0x12;  //消息ID
    data_send[2] = 0x01;  //数据长度：1
    data_send[3] = 0x01;  //机械爪停止
    data_send[4] = data_send[1] ^ data_send[2] ^ data_send[3];
    data_send[5] = 0xFF;
    emit data_upup->signal_send_JiXieBi_hand(data_send);
}


void page1::on_pushButton_hand_zhang_clicked()
{
    QByteArray data_send;
    data_send.resize(6);
    data_send[0] = 0xFE;  //数据头
    data_send[1] = 0x12;  //消息ID
    data_send[2] = 0x01;  //数据长度：1
    data_send[3] = 0x02;  //机械爪张开
    data_send[4] = data_send[1] ^ data_send[2] ^ data_send[3];
    data_send[5] = 0xFF;
    emit data_upup->signal_send_JiXieBi_hand(data_send);
}


void page1::on_pushButton_light_open_clicked()
{
    QByteArray data_send;
    data_send.resize(6);
    data_send[0] = 0xFE;  //数据头
    data_send[1] = 0x15;  //消息ID
    data_send[2] = 0x01;  //数据长度：1
    data_send[3] = 0x01;  //打开照明灯
    data_send[4] = data_send[1] ^ data_send[2] ^ data_send[3];
    data_send[5] = 0xFF;
    emit data_upup->signal_send_Light(data_send);
}


void page1::on_pushButton_light_close_clicked()
{
    QByteArray data_send;
    data_send.resize(6);
    data_send[0] = 0xFE;  //数据头
    data_send[1] = 0x15;  //消息ID
    data_send[2] = 0x01;  //数据长度：1
    data_send[3] = 0x02;  //关闭照明灯
    data_send[4] = data_send[1] ^ data_send[2] ^ data_send[3];
    data_send[5] = 0xFF;
    emit data_upup->signal_send_Light(data_send);
}


void page1::on_pushButton_camera_lock_clicked()
{
    QByteArray data_send;
    data_send.resize(6);
    data_send[0] = 0xFE;  //数据头
    data_send[1] = 0x14;  //消息ID
    data_send[2] = 0x01;  //数据长度：1
    data_send[3] = 0x01;  //云台锁定
    data_send[4] = data_send[1] ^ data_send[2] ^ data_send[3];
    data_send[5] = 0xFF;
    emit data_upup->signal_send_YunTai(data_send);
}


void page1::on_pushButton_camera_unlock_clicked()
{
    QByteArray data_send;
    data_send.resize(6);
    data_send[0] = 0xFE;  //数据头
    data_send[1] = 0x14;  //消息ID
    data_send[2] = 0x01;  //数据长度：1
    data_send[3] = 0x02;  //云台解锁
    data_send[4] = data_send[1] ^ data_send[2] ^ data_send[3];
    data_send[5] = 0xFF;
    emit data_upup->signal_send_YunTai(data_send);
}


void page1::on_pushButton_cmaera_return_clicked()
{
    QByteArray data_send;
    data_send.resize(6);
    data_send[0] = 0xFE;  //数据头
    data_send[1] = 0x14;  //消息ID
    data_send[2] = 0x01;  //数据长度：1
    data_send[3] = 0x03;  //云台复位
    data_send[4] = data_send[1] ^ data_send[2] ^ data_send[3];
    data_send[5] = 0xFF;
    emit data_upup->signal_send_YunTai(data_send);
}


void page1::on_pushButton_camera_turn_clicked()
{
    qint16 camera_x = (qint16)(ui->lineEdit_camera_x->text().toDouble()*10000);
    qint16 camera_y = (qint16)(ui->lineEdit_camera_y->text().toDouble()*10000);

    QByteArray data_send;
    data_send.resize(10);
    data_send[0] = 0xFE;  //数据头
    data_send[1] = 0x14;  //消息ID
    data_send[2] = 0x05;  //数据长度：5
    //云台转动
    data_send[3] = 0x04;
    //传入x轴数据
    data_send[4] = camera_x;
    data_send[5] = camera_x>>8;
    //传入y轴数据
    data_send[6] = camera_y;
    data_send[7] = camera_y>>8;
    //校验位
    data_send[8] = data_send[1];
    for(int i = 2;i <= 7;i++){
        data_send[8] = data_send[8] ^ data_send[i];
    }
    //数据尾
    data_send[9] = 0xFF;
    emit data_upup->signal_send_YunTai(data_send);
}


void page1::on_pushButton_pwm_clicked()
{
    qint16 pwm1 = (qint16)(ui->lineEdit_pwm1->text().toInt());
    qint16 pwm2 = (qint16)(ui->lineEdit_pwm2->text().toInt());
    qint16 pwm3 = (qint16)(ui->lineEdit_pwm3->text().toInt());
    qint16 pwm4 = (qint16)(ui->lineEdit_pwm4->text().toInt());
    qint16 pwm5 = (qint16)(ui->lineEdit_pwm5->text().toInt());
    qint16 pwm6 = (qint16)(ui->lineEdit_pwm6->text().toInt());
    qint16 pwm7 = (qint16)(ui->lineEdit_pwm7->text().toInt());
    qint16 pwm8 = (qint16)(ui->lineEdit_pwm8->text().toInt());

    QByteArray data_send;
    data_send.resize(21);
// resize长度要比用到的data_send[20]大1
//    qDebug()<<"debug_send_upup 长度:"<<data_send.size();
//    qDebug()<<"debug_send_upup"<<(unsigned char)data_send.at(20);
    data_send[0] = 0xFE;  //数据头
    data_send[1] = 0x16;  //消息ID
    data_send[2] = 0x16;  //数据长度：16

    //传入pwm1
    data_send[3] = pwm1;
    data_send[4] = pwm1>>8;
    //传入pwm2
    data_send[5] = pwm2;
    data_send[6] = pwm2>>8;
    //传入pwm3
    data_send[7] = pwm3;
    data_send[8] = pwm3>>8;
    //传入pwm4
    data_send[9] = pwm4;
    data_send[10] = pwm4>>8;
    //传入pwm5
    data_send[11] = pwm5;
    data_send[12] = pwm5>>8;
    //传入pwm6
    data_send[13] = pwm6;
    data_send[14] = pwm6>>8;
    //传入pwm7
    data_send[15] = pwm7;
    data_send[16] = pwm7>>8;
    //传入pwm8
    data_send[17] = pwm8;
    data_send[18] = pwm8>>8;

    //校验位
    data_send[19] = data_send[1];
    for(int i = 2;i <= 18;i++){
        data_send[19] = data_send[19] ^ data_send[i];
    }

    //数据尾
    data_send[20] = 0xFF;
//    debug_print(data_send);
    emit data_upup->signal_send_YunTai(data_send);
}

void page1::debug_print(QByteArray debug_array)
{
    int size = debug_array.size();
    for(int i = 0; i<size ;i++){
        qDebug()<<"debug : data["<<i<<"] = "<<(unsigned char)debug_array.at(i);
    }
    qDebug()<<"\n";
}

//################################################
//#                                              #
//#           修改ui里面的值                       #
//#           最后一次修改4.24，23：17              #
//#                                              #
//################################################

void page1::set_lineEdit_pingdong_x(double num)
{
    ui->lineEdit_pingdong_x->setText(QString::number(num));
}

void page1::set_lineEdit_pingdong_y(double num)
{
    ui->lineEdit_pingdong_y->setText(QString::number(num));
}

void page1::set_lineEdit_pingdong_z(double num)
{
    ui->lineEdit_pingdong_z->setText(QString::number(num));
}

void page1::set_lineEdit_zhuandong_roll(double num)
{
    ui->lineEdit_zhuandong_roll->setText(QString::number(num));
}

void page1::set_lineEdit_zhuandong_pitch(double num)
{
    ui->lineEdit_zhuandong_pitch->setText(QString::number(num));
}

void page1::set_lineEdit_zhuandong_yaw(double num)
{
    ui->lineEdit_zhuandong_yaw->setText(QString::number(num));
}



